package com.sell.controller;

import com.sell.config.CookieConfig;
import com.sell.config.ProjectUrlConfig;
import com.sell.config.RedisConfig;
import com.sell.dataobject.SellerInfo;
import com.sell.enums.ResultEnum;
import com.sell.exception.SellException;
import com.sell.service.SellerService;
import com.sell.utils.CookieUtils;
import com.sell.utils.KeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @author: Xukai
 * @description: 卖家用户
 * @createDate: 2018/7/9 17:40
 * @modified By:
 */
@Controller
@RequestMapping("/seller")
@Slf4j
public class SellerUserController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    @Autowired
    private RedisConfig redisConfig;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private CookieConfig cookieConfig;

    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        //1. 将openid和数据库的数据匹配
        SellerInfo sellerInfo;
        try {
            sellerInfo = sellerService.findSellerInfoByOpenid(openid);
        } catch (SellException e) {
            log.error("【卖家端登录】发生异常{}", e);
            mav.addObject("msg", ResultEnum.LOGIN_FAIL.getMessage());
            mav.addObject("url", "/sell/seller/order/list");
            mav.setViewName("common/error");
            return mav;
        }
        //2. 设置token至redis
        String token = KeyUtils.genUUID();
        Integer expire = redisConfig.getExpire();
        redisTemplate.opsForValue().set(String.format(redisConfig.getToken_prefix(), token), openid, expire, TimeUnit.SECONDS);
        //3. 设置token至cookie
        CookieUtils.set(cookieConfig.getToken(), token, cookieConfig.getExpire(), response);
        mav.setViewName("redirect:" + projectUrlConfig.getSell() + "/sell/seller/order/list");
        return mav;
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        //1. 从cookie里查询
        Cookie cookie = CookieUtils.get(cookieConfig.getToken(), request);
        if (cookie != null) {
            //2. 清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(redisConfig.getToken_prefix(), cookie.getValue()));
            //3. 清除cookie
            CookieUtils.set(cookieConfig.getToken(), null, 0, response);
        }
        mav.addObject("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
        mav.addObject("url", "/sell/seller/order/list");
        mav.setViewName("common/success");
        return mav;
    }

}
